package com.chongjing.chat.service.impl;

import java.util.List;

import cn.wildfirechat.proto.WFCMessage;
import com.chongjing.chat.domain.MessageConditionData;
import com.chongjing.chat.domain.MessageResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.chongjing.chat.mapper.MessageMapper;
import com.chongjing.chat.service.IMessageService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author zhoutianyu
 * 2025-02-05
 */
@Service
@Slf4j
public class IMessageServiceImpl implements IMessageService
{
    @Autowired
    private MessageMapper messageMapper;

    @Override
    public List<MessageResponse> page(MessageConditionData messageConditionData) {
        List<MessageResponse> messages = messageMapper.page(messageConditionData);
        // 对每条消息的_data字段进行解析
        messages.forEach(message -> {
            //1. 获取Blob数据
            byte[] data = message.get_data(); // getData方法返回_data字段的byte[]
//            System.out.println(data);
            byte[] decryptedMessageContentBytes = encryptMessageContent(data, false);
            try {
                WFCMessage.MessageContent messageContent = WFCMessage.MessageContent.parseFrom(decryptedMessageContentBytes);
//                System.out.println(messageContent);
                if (message.get_content_type()==5 || message.get_content_type()==6 ||message.get_content_type()==3 ||message.get_content_type()==7 ||message.get_content_type()==2 ||message.get_content_type()==23){
                    message.set_searchable_key(messageContent.getRemoteMediaUrl());
                }

            } catch (Exception e) {
                // 处理可能的异常
                e.printStackTrace();
            }
        });
        return messages;
    }

    @Override
    public MessageResponse getById(Long id) {
        MessageResponse response = messageMapper.getById(id);
//        log.info("查询结果：{}", response);  // 添加日志
//        if (response != null) {
//            log.info("ID: {}, Type: {}, ContentType: {}, SenderName: {}, ReceiverName: {}",
//                    response.get_mid(), response.get_type(), response.get_content_type(),
//                    response.getSender_name(), response.getReceiver_name());
//        }
        //1. 获取Blob数据
        byte[] data = response.get_data(); // getData方法返回_data字段的byte[]
        byte[] decryptedMessageContentBytes = encryptMessageContent(data, false);
        try {
            WFCMessage.MessageContent messageContent = WFCMessage.MessageContent.parseFrom(decryptedMessageContentBytes);
//            System.out.println(messageContent);
            if (response.get_content_type()==5 || response.get_content_type()==6 ||response.get_content_type()==3 ||response.get_content_type()==7 ||response.get_content_type()==2 ||response.get_content_type()==23){
                response.set_searchable_key(messageContent.getRemoteMediaUrl());
            }
        } catch (Exception e) {
            // 处理可能的异常
            e.printStackTrace();
        }
        return response ;
    }

    @Override
    public void deleteMessage(String ids) {
        if (ids != null && !ids.isEmpty()) {
            String[] idArray = ids.split(","); // 将字符串分割成数组
            for (String id : idArray) {
                String[] parts = id.split("_"); // 分割 ID 和表名
                String messageId = parts[0]; // 消息 ID
                String tableName = "t_messages_" + parts[1]; // 表名
                messageMapper.deleteMessage(messageId, tableName); // 调用服务层删除消息
            }
        }
    }

    byte[] encryptMessageContent(byte[] in, boolean force) {
        if(in != null && ( force)) {
            for (int i = 0; i < in.length; i++) {
                in[i] ^= 0xBD;
            }
        }
        return in;
    }

}
